package base.lib.widget.Floating;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

/**
 * 自定义的View，实现ListView A~Z快速索引效果
 *
 * @author Folyd
 *
 */
public class IndexBarView extends View {
    // 是否画出背景
    private boolean showBg = false;

    private OnTouchLetterChangeListenner listenner;
    // 选中的项
    private int choose;

    private int colorDefault; //字体颜色
    private int colorhighlighted;   //高亮
    private int colorHoverColor ;   //背景
    private float floatTextSize; //字体大小

    // 准备好的A~Z的字母数组
//    public static String[] letters = { "#", "A", "B", "C", "D", "E", "F", "G",
//            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
//            "U", "V", "W", "X", "Y", "Z" };

    private ArrayList<String> arrayListLetters;

    public void setLetters(ArrayList<String> letters){
        arrayListLetters = letters;
        invalidate();
    }

    // 初始化
    private void _init(){
        arrayListLetters = new ArrayList<String>();
        choose = -1;
        colorDefault = Color.parseColor("#666666");
        colorhighlighted = Color.parseColor("#FFFFFF");
        colorHoverColor = Color.parseColor("#55000000");
        floatTextSize = 25f;
    }

    private int getHoverColor() {
        return colorHoverColor;
    }

    public void setHoverColor(int colorDefault) {
        this.colorHoverColor = colorDefault;
    }

    // 构造方法
    public IndexBarView(Context context) {
        super(context);
        _init();
    }

    public IndexBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        _init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (arrayListLetters == null || arrayListLetters.size() == 0){
            return;
        }
        // 获取宽和高
        int width = getWidth();
        int height = getHeight();
        // 每个字母的高度
        int singleHeight = height / arrayListLetters.size();
        if (showBg) {
            // 画出背景
            canvas.drawColor(getHoverColor());
        }
        Paint paint = new Paint();
        // 设置字体格式
        paint.setTypeface(Typeface.DEFAULT);
        paint.setAntiAlias(true);
        paint.setTextSize(floatTextSize);
        paint.setColor(colorDefault);

        Paint paintHighlighted = null;

        // 画字母
        for (int i = 0; i < arrayListLetters.size(); i++) {

            String title = arrayListLetters.get(i);
            // 要画的字母的x,y坐标
            float posX = width / 2 - paint.measureText(title) / 2;
            float posY = i * singleHeight + (singleHeight - (singleHeight-paint.measureText(title))/2);

            // 如果这一项被选中，则换一种颜色画
            if (i == choose) {
                if (paintHighlighted == null){
                    paintHighlighted = new Paint();
                    // 设置字体格式
                    paintHighlighted.setTypeface(Typeface.DEFAULT);
                    paintHighlighted.setAntiAlias(true);
                    paintHighlighted.setTextSize(floatTextSize);
                    paintHighlighted.setColor(colorhighlighted);
                }
                // 画出字母
                canvas.drawText(title, posX, posY, paintHighlighted);
            }else {
                // 画出字母
                canvas.drawText(title, posX, posY, paint);
            }
        }
    }

    /**
     * 处理SlideBar的状态
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (arrayListLetters == null || arrayListLetters.size() == 0 || listenner == null){
            return true;
        }
        final float y = event.getY();
        // 算出点击的字母的索引
        final int index = (int) (y / getHeight() * arrayListLetters.size());
        if (index < 0 || index >= arrayListLetters.size()){
            return true;
        }
        // 保存上次点击的字母的索引到oldChoose
        final int oldChoose = choose;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                showBg = true;
                if (oldChoose != index) {
                    choose = index;
                    listenner.onTouchLetterChange(showBg, arrayListLetters.get(index), index);
                    invalidate();
                }
                break;

            case MotionEvent.ACTION_MOVE:
                if (oldChoose != index) {
                    choose = index;
                    listenner.onTouchLetterChange(showBg, arrayListLetters.get(index), index);
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                showBg = false;
                choose = -1;
                listenner.onTouchLetterChange(showBg, arrayListLetters.get(index), index);
                invalidate();
                break;
        }
        return true;
    }

    /**
     * 回调方法，注册监听器
     *
     * @param listenner
     */
    public void setOnTouchLetterChangeListenner(
            OnTouchLetterChangeListenner listenner) {
        this.listenner = listenner;
    }

    /**
     * SlideBar 的监听器接口
     *
     * @author Folyd
     *
     */
    public interface OnTouchLetterChangeListenner {
        void onTouchLetterChange(boolean isTouched, String s, int index);
    }

}
